﻿using System.Collections.Generic;
using Business;

public partial class Controls_BackEnd_Header : System.Web.UI.UserControl
{
    private string GetSubmenu(Functions functions)
    {
        int id;
        int.TryParse(Request["target"], out id);
        int cti;
        int.TryParse(Request["id"], out cti);
        var list = (List<Functions>)Session[SessionManager.SessionFunctions];
        var listsub = list.FindAll(f => f.FatherID == functions.FunctionID);
        listsub.Sort((f1, f2) => f1.Order.CompareTo(f2.Order));
        var html = "<ul class=\"" + (functions.FunctionID == id ? "current" : "select") + "\">" +
                   "<li><a href=\"javascript:void(0);\"><b>" + functions.Name + "</b></a>" +
                   "<div class=\"select_sub" + (functions.FunctionID == id ? " show" : string.Empty) + "\">" +
                   "<ul class=\"sub\">";
        foreach (var sub in listsub)
        {
            var father = list.Find(f => f.FunctionID == cti);
            html += "<li " + (sub.FunctionID == cti || (father != null && sub.FunctionID == father.FatherID) ? " class=\"sub_show\"" : "onclick=\" Loading(100, 100, '');\"") + "><a href=\"" + (sub.FunctionID != cti ? Config.ApplicationUrl + functions.FunctionID + "/" + sub.FunctionID + "/admin.html" : "javascript:void(0);") + "\">" +
                    sub.Name + "</a></li>";

        }
        html += "</ul>" +
                "</div>" +
                "</li>" +
                "</ul>";
        return html;

    }
    public string Generate()
    {
        var html = string.Empty;
        var functions = (List<Functions>)Session[SessionManager.SessionFunctions];
        var tabs = functions.FindAll(f => f.FatherID == 0);
        tabs.Sort((f1, f2) => f1.Order.CompareTo(f2.Order));
        var index = 1;
        foreach (var tab in tabs)
        {
            html += GetSubmenu(tab);
            if (index < tabs.Count)
                html += "<div class=\"nav-divider\">&nbsp;</div>";
            index++;
        }
        return string.Format("{0}</ul>", html);
    }
}
